﻿# Animation.py: the animation module
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Sunday, June 25, 2009 22:45:00 PM
# Python 2.6.2

import Sprites
import Timer

'''
Animation.py:

Animation types:

* half-pass: iterate once with a step of 1
* halfreverse-pass: iterate once with a step of -1
* full-pass: iterate once forth and back, step of 1 then when done -1

'''

class Animation:
    def __init__(self, name, pos, frames, frame_times, anim_type='half-pass', infinite=False):
        self.frame_prefix = name #e.g. ('data', 'enemies', 'death') frame one is: death0.png
        self.pos = pos #position on screen
        
        self.frames = frames #number of frames
        self.frame_times = frame_times #list with the duration of each frame
        self.anim_type = anim_type
        self.infinite = infinite #never stops? resets to start when it ends
        
        assert len(self.frame_times) == self.frames
        self.reset()
        
    def update_path(self):
        piece2 = (''.join((self.frame_prefix[-1], str(self.current_frame))),)
        self.path = '\\'.join((self.frame_prefix[:-1] + piece2))
                
                
    def update(self):
        # Time control code   
        if self.playing:
            if self.time_passed < 0:
                self.time_passed = 0
            else:
                self.time_passed += Timer.Timer.get_time_variation()
                if self.time_passed >= self.frame_times[self.current_frame]:
                    # change frame or finish anim
                    self.time_passed = 0
                    self.current_frame += self.step
                    if not 0 <= self.current_frame < self.frames:
                        self.current_frame -= self.step #returns to valid frame
                        self.finished()
                        
                    self.update_path()
                
        
               
    def draw(self, dest):
        ''' Draws animation and changes frames if it's running. '''
        self.update()                

        # Draw current frame
        dest.blit(Sprites.Sprites.get_sprite(self.path), self.pos)
            
    
    def reset(self):
        ''' Resets to initial animation status. '''
        if self.anim_type == 'halfreverse-pass':
            self.current_frame = (self.frames - 1)
            self.step = -1
        else:
            self.current_frame = 0
            self.step = 1
        
        self.time_passed = -1 #will be incremented each frame with Timer.get_time_variation()
        self.update_path()
        self.playing = False
            
    
    def finished(self):
        ''' Called when the current frame is not in the frame range. Decides whether to repeat backwards, stop, etc. '''
        end = False
        if self.anim_type == 'half-pass':
            end = True
        elif self.anim_type == 'halfreverse-pass':
            self.stop()
        elif self.anim_type == 'full-pass':
            if self.current_frame >= (self.frames - 1):
                self.step = -1
                #for full-pass, move to next frame, dont display this frame twice!
                self.current_frame += self.step
            else:
                end = True
        
        if end:
            if self.infinite: self.play()
            else: self.stop()
        
                
    def stop(self):
        ''' Stops animation. '''
        self.playing = False
    
    
    def play(self):
        ''' Resets animation and starts again. '''
        self.reset()
        self.playing = True
    
    
    def is_playing(self):
        ''' Is the animation playing? '''
        return self.playing
    
    
    
    
    
        
        





